#include "ParticleManager.h"
#include <stdio.h>

ParticleManager* ParticleManager::GetInstance(void)
{
	static ParticleManager Instance;
	return &Instance;
}

bool ParticleManager::Load(string filename)
{
	TiXmlDocument doc;

	if(doc.LoadFile(filename.c_str()) == false)
		return false;

	ParticleMap[filename] = new Emitter();

	TiXmlElement* pRoot = doc.RootElement();

	TiXmlElement* Life = pRoot->FirstChildElement("Life");
	if(Life)
	{
		int att = -1;

		if(Life->Attribute("MinLife",&att) != NULL)
		{
			ParticleMap[filename]->SetMinLifeSpan((float)att);
		}
		else 
			return false;

		if(Life->Attribute("MaxLife",&att) != NULL)
		{
			ParticleMap[filename]->SetMaxLifeSpan((float)att);
		}
		else 
			return false;

		if(Life->Attribute("MaxParticles",&att) != NULL)
		{
			ParticleMap[filename]->SetMaxParticles(att);
			ParticleMap[filename]->SetParticles(ParticleMap[filename]->GetMaxParticles());
		}
		else 
			return false;
	}
	else 
		return false;

	TiXmlElement* Size = pRoot->FirstChildElement("Size");

	if(Size)
	{
		double att;

		if(Size->Attribute("StartScale",&att) != NULL)
		{
			ParticleMap[filename]->SetStartScale(float(att));
		}
		else 
			return false;

		if(Size->Attribute("EndScale",&att) != NULL)
		{
			ParticleMap[filename]->SetEndScale(float(att));
		}
		else 
			return false;
	}
	else 
		return false;

	TiXmlElement* Velocity = pRoot->FirstChildElement("Velocity");

	if(Velocity)
	{
		double att = -1;

		if(Velocity->Attribute("XVelocity1",&att) != NULL)
		{
			ParticleMap[filename]->SetVelocityX1((float)att);
		}
		else 
			return false;

		if(Velocity->Attribute("XVelocity2",&att) != NULL)
		{
			ParticleMap[filename]->SetVelocityX2((float)att);
		}
		else 
			return false;

		if(Velocity->Attribute("YVelocity1",&att) != NULL)
		{
			ParticleMap[filename]->SetVelocityY1((float)att);
		}
		else 
			return false;

		if(Velocity->Attribute("YVelocity2",&att) != NULL)
		{
			ParticleMap[filename]->SetVelocityY2((float)att);
		}
		else 
			return false;
	}
	else 
		return false;

	TiXmlElement* Movement = pRoot->FirstChildElement("Movement");

	if(Movement)
	{
		double att;

		if(Movement->Attribute("Rotation",&att) != NULL)
		{
			ParticleMap[filename]->SetRotation((float)att);
		}
		else 
			return false;

		if(Movement->Attribute("GravityX",&att) != NULL)
		{
			ParticleMap[filename]->SetGravityX((float)att);
		}
		else 
			return false;

		if(Movement->Attribute("GravityY",&att) != NULL)
		{
			ParticleMap[filename]->SetGravityY((float)att);
		}
		else 
			return false;
	}
	else 
		return false;

	TiXmlElement* Blend = pRoot->FirstChildElement("Blend");

	if(Blend)
	{
		int att = -1;

		if(Blend->Attribute("SourceBlend",&att) != NULL)
		{
			ParticleMap[filename]->SetSrcBlend(att);
		}
		else 
			return false;

		if(Blend->Attribute("DestBlend",&att) != NULL)
		{
			ParticleMap[filename]->SetDestBlend(att);
		}
		else 
			return false;
	}
	else 
		return false;

	TiXmlElement* Loop = pRoot->FirstChildElement("Loop");

	if(Loop)
	{
		int att = -1;

		if(Loop->Attribute("Continous") != NULL)
		{
			if(Loop->Attribute("Continous",&att))
			{
				ParticleMap[filename]->SetLoop(att);
			}
			else
				return false;
		}
		else 
			return false;
	}
	else
		return false;

	TiXmlElement* Rect = pRoot->FirstChildElement("Rect");

	if(Rect)
	{
		double att = -1;

		if(Rect->Attribute("Left",&att) != NULL)
		{
			ParticleMap[filename]->SetRectL((long)att);
		}
		else 
			return false;

		if(Rect->Attribute("Right",&att) != NULL)
		{
			ParticleMap[filename]->SetRectR((long)att);
		}
		else 
			return false;

		if(Rect->Attribute("Top",&att) != NULL)
		{
			ParticleMap[filename]->SetRectT((long)att);
		}
		else 
			return false;

		if(Rect->Attribute("Bottom",&att) != NULL)
		{
			ParticleMap[filename]->SetRectB((long)att);
		}
		else 
			return false;

		if(Rect->Attribute("PosX",&att) != NULL)
		{
			ParticleMap[filename]->SetPosX((float)att);
		}
		else 
			return false;

		if(Rect->Attribute("PosY",&att) != NULL)
		{
			ParticleMap[filename]->SetPosY((float)att);
		}
		else 
			return false;
	}
	else
		return false;

	TiXmlElement* Color = pRoot->FirstChildElement("Color");

	if(Color)
	{
		double att = -1;

		if(Color->Attribute("StartColorR",&att) != NULL)
		{
			ParticleMap[filename]->SetStartColorR((float)att);
		}
		else 
			return false;

		if(Color->Attribute("StartColorG",&att) != NULL)
		{
			ParticleMap[filename]->SetStartColorG((float)att);
		}
		else 
			return false;

		if(Color->Attribute("StartColorB",&att) != NULL)
		{
			ParticleMap[filename]->SetStartColorB((float)att);
		}
		else 
			return false;

		if(Color->Attribute("EndColorR",&att) != NULL)
		{
			ParticleMap[filename]->SetEndColorR((float)att);
		}
		else 
			return false;

		if(Color->Attribute("EndColorG",&att) != NULL)
		{
			ParticleMap[filename]->SetEndColorG((float)att);
		}
		else 
			return false;

		if(Color->Attribute("EndColorB",&att) != NULL)
		{
			ParticleMap[filename]->SetEndColorB((float)att);
		}
		else 
			return false;

		if(Color->Attribute("StartAlpha",&att) != NULL)
		{
			ParticleMap[filename]->SetStartColorA((float)att);
		}
		else
			return false;

		if(Color->Attribute("EndAlpha",&att) != NULL)
		{
			ParticleMap[filename]->SetEndColorA((float)att);
		}
		else
			return false;
	}
	else
		return false;

	TiXmlElement* Image = pRoot->FirstChildElement("Image");

	if(Image)
	{
		if(Image->Attribute("ParticleImage") != NULL)
		{
			string RelativePath = "resources/graphics/";
			ParticleMap[filename]->SetFileName(Image->Attribute("ParticleImage"));
			string Image = RelativePath + ParticleMap[filename]->GetImageFile();
			ParticleMap[filename]->SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture(Image.c_str()));
		}
		else
			return false;
	}
	else 
		return false;

	int size = ParticleMap[filename]->GetMaxParticles();

	//TiXmlElement* Particle = pRoot->FirstChildElement("Particles");

	for(int i = 0; i < size; i++)
	{
		char Element[20] = {0};
		sprintf_s(Element,"Particles%d",i);

		TiXmlElement* Particles = pRoot->FirstChildElement(Element);

		double att = -1;

		if(Particles)
		{

			if(Particles->Attribute("MinLife",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_fCurrentLife = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("LifeTime",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_fLifeTime = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("Scale",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_fScale = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("VelocityX",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_vVelocity.fX = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("VelocityY",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_vVelocity.fY = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("PartRotation",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_fRotation = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("CurrColorR",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_cColor.r = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("CurrColorG",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_cColor.g = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("CurrColorB",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_cColor.b = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("CurrColorA",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_cColor.a = (float)att;
			}
			else
				return false;

			if(Particles->Attribute("StartPositionX",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_vStartPosition.fX = (float)att;
				ParticleMap[filename]->GetParticles(i)->m_vCurrentPosition.fX = ParticleMap[filename]->GetParticles(i)->m_vStartPosition.fX;
			}
			else
				return false;

			if(Particles->Attribute("StartPositionY",&att) != NULL)
			{
				ParticleMap[filename]->GetParticles(i)->m_vStartPosition.fY = (float)att;
				ParticleMap[filename]->GetParticles(i)->m_vCurrentPosition.fY = ParticleMap[filename]->GetParticles(i)->m_vStartPosition.fY;
			}
			else
				return false;

			ParticleMap[filename]->GetParticles(i)->m_bIsAlive = true;

		}
		else
			return false;
	}

	ParticleFile = filename;
	return true;
}

void ParticleManager::Update(float ElapsedTime)
{
	ParticleMap[ParticleFile]->Update(ElapsedTime);
}

void ParticleManager::Render(void)
{
	ParticleMap[ParticleFile]->Render();
}

void ParticleManager::AddRef(void)
{
	m_uiRefCount++;
}

void ParticleManager::Release(void)
{
	m_uiRefCount--;

		if (m_uiRefCount == 0)
			delete this;
}

ParticleManager::~ParticleManager()
{
	int size = ParticleMap[ParticleFile]->GetMaxParticles();

	for(int i = 0; i < size; i++)
	{
		delete ParticleMap[ParticleFile]->GetParticles(i);
	}

	delete ParticleMap[ParticleFile];
}